package chatRoom1;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;

public class ChatReceiver implements Runnable {
    private DatagramSocket server;
    public ChatReceiver(DatagramSocket server) {
        this.server = server;
    }
    @Override
    public void run() {
        try {
            byte[] buf = new byte[1024];
            DatagramPacket packet = new DatagramPacket(buf, buf.length);
            while (true) {
                server.receive(packet);
                String str = new String(packet.getData(), 0, packet.getLength());
                System.out.println("接收" + packet.getAddress() + ":"
                        +packet.getPort() + "发送来的数据：" + str);
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}